<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Comments Model
 * @description : Database functions related to user comments
 * 
 */
class Comment_model extends CI_Model {

  function __construct() {
    parent::__construct();
  }

  // Insert a new comment
  function add_comment($comment_type = NULL, $comment_type_id = NULL, $user_id = NULL, $comment_message = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($comment_type) && !empty($comment_type_id) && is_numeric($comment_type_id) && !empty($user_id) && is_numeric($user_id) && !empty($comment_message)) {
      $query_insr = array(
          'comment_type' => $comment_type,
          'comment_type_id' => $comment_type_id,
          'comment_user_id' => $user_id,
          'comment_message' => $comment_message,
          'comment_date' => date("Y-m-d H:i:s"),
          'comment_status' => 1
      );
      $query_exec = $this->db->insert('comments', $query_insr);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->insert_id();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  // Get comments of a section by id (Polls, Photos, etc..)
  function get_comments($comment_type = NULL, $comment_type_id = NULL, $limit = 10) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($comment_type) && !empty($comment_type_id) && is_numeric($comment_type_id)) {
      $query_cond = array();
      $query_cond['comment_type'] = $comment_type;
      $query_cond['comment_type_id'] = $comment_type_id;
      $query_cond['comment_status'] = 1;
      $query_cond['user_status'] = 1;
      $this->db->join('users', 'user_id = comment_user_id');
      $this->db->order_by('comment_id', 'desc');
      $query_exec = $this->db->get_where('comments', $query_cond, $limit);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get deleted comments (ie. comment status is 0) of a section by id (Polls, Photos, etc..)
  function get_deleted_comments($comment_type = NULL, $comment_type_id = NULL, $limit = 10) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($comment_type) && !empty($comment_type_id) && is_numeric($comment_type_id)) {
      $query_cond = array();
      $query_cond['comment_type'] = $comment_type;
      $query_cond['comment_type_id'] = $comment_type_id;
      $query_cond['comment_status'] = 0;
      $this->db->join('users', 'user_id = comment_user_id');
      $this->db->order_by('comment_id', 'desc');
      $query_exec = $this->db->get_where('comments', $query_cond, $limit);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get comments of a user by user id
  function get_my_comments($user_id = NULL, $limit = 10) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($user_id) && is_numeric($user_id)) {
      $query_cond = array();
      $query_cond['comment_user_id'] = $user_id;
      $query_cond['comment_status'] = 1;
      $this->db->order_by('comment_id', 'desc');
      $query_exec = $this->db->get_where('comments', $query_cond, $limit);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get deails of a comment by comment id
  function get_comment_by_id($comment_id = NULL, $comment_status = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($comment_id) && is_numeric($comment_id)) {
      $query_cond = array();
      $query_cond['comment_id'] = $comment_id;
      $query_cond['comment_status'] = $comment_status;
      $this->db->join('users', 'user_id = comment_user_id');
      $query_exec = $this->db->get_where('comments', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Delete a comment using comment id and user id
  function del_comment($comment_id = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($comment_id) && is_numeric($comment_id) && !empty($user_id) && is_numeric($user_id)) {
      $query_upda = array();
      $query_upda['comment_status'] = 0;
      $query_cond = array(
          'comment_id' => $comment_id,
          'comment_user_id' => $user_id
      );
      $query_exec = $this->db->update('comments', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  // Delete a comment using comment id
  function del_comment_by_id($comment_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($comment_id) && is_numeric($comment_id)) {
      $query_upda = array();
      $query_upda['comment_status'] = 0;
      $query_cond = array(
          'comment_id' => $comment_id
      );
      $query_exec = $this->db->update('comments', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  // Get new comments (For ajax) by providing last shown comment id
  function get_new_comments($comment_type = NULL, $comment_type_id = NULL, $comment_last_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($comment_type) && !empty($comment_type_id) && is_numeric($comment_type_id) && is_numeric($comment_last_id)) {
      $query_cond = array();
      $query_cond['comment_type'] = $comment_type;
      $query_cond['comment_type_id'] = $comment_type_id;
      $query_cond['comment_status'] = 1;
      $this->db->join('users', 'user_id = comment_user_id');
      $this->db->order_by('comment_id', 'desc');
      $this->db->where('comment_id >', $comment_last_id);
      $query_exec = $this->db->get_where('comments', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get old comments (For ajax) by providing last loaded comment id
  function get_old_comments($comment_type = NULL, $comment_type_id = NULL, $comment_last_loaded_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($comment_type) && !empty($comment_type_id) && is_numeric($comment_type_id) && is_numeric($comment_last_loaded_id)) {
      $query_cond = array();
      $query_cond['comment_type'] = $comment_type;
      $query_cond['comment_type_id'] = $comment_type_id;
      $query_cond['comment_status'] = 1;
      $this->db->join('users', 'user_id = comment_user_id');
      $this->db->order_by('comment_id', 'desc');
      $this->db->where('comment_id <', $comment_last_loaded_id);
      $query_exec = $this->db->get_where('comments', $query_cond, 10);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get recent comments of an album photos
  function get_album_photo_comments($album_id = NULL, $limit = 10) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($album_id) && is_numeric($album_id)) {
      $query_sql = "SELECT * FROM (`comments`) JOIN `users` ON `user_id` = `comment_user_id` JOIN `albums_photos` ON `photo_id` = `comment_type_id` WHERE (comment_type = 'photo' OR comment_type = 'video') AND `photo_album_id` = '{$album_id}' AND `comment_status` = 1 ORDER BY `comment_id` desc LIMIT {$limit}";
      $query_exec = $this->db->query($query_sql);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Delete all comments of a post
  function del_comment_by_post($comment_type = NULL, $comment_type_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($comment_type) && !empty($comment_type_id) && is_numeric($comment_type_id)) {
      $query_upda = array();
      $query_upda['comment_status'] = 0;
      $query_cond = array(
          'comment_type' => $comment_type,
          'comment_type_id' => $comment_type_id,
      );
      $query_exec = $this->db->update('comments', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

}

?>